/**
 * Copyright Dan Danciu, March 2008.
 */
package ro.dandanciu.webgallery.domainmodel;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Enumerated;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import ro.dandanciu.webgallery.domainmodel.utils.AccountStatus;
import ro.dandanciu.webgallery.domainmodel.utils.DbConstants;

/**
 * Domain class for users.
 * @author Dan
 */
@Entity
@Table(name = "TA_USER")
@NamedQueries({
        @NamedQuery(name = "User.GetAll", query = "select u from User u "
                        + "where u.userId LIKE :filter or u.userName LIKE :filter") })
public class User implements Comparable < User > {

    /**
     * The user id, used to login. Mapped to K_USER.
     */
    @Id
    @Column(name = "K_USER", length = DbConstants.User.USER_ID_SIZE)
    private String userId;

    /**
     * The full name of the user Mapped to C_USERNAME.
     */
    @Column(name = "C_USERNAME", length = DbConstants.User.USER_NAME_SIZE)
    private String userName;

    /**
     * The password, used at login. Mapped to C_PASSWORD.
     */
    @Column(name = "C_PASSWORD", length = DbConstants.User.PASSWORD_SIZE)
    private String password;

    /**
     * The email of user. Mapped to C_EMAIL.
     */
    @Column(name = "C_EMAIL", length = DbConstants.User.EMAIL_SIZE)
    private String email;

    /**
     * The users language, mapped to C_LANG.
     */
    @Column(name = "C_LANG", length = DbConstants.User.LANGUAGE_SIZE)
    private String language;

    /**
     * Is the user administrator. Mapped to B_ADMIN.
     */
    @Column(name = "B_ADMIN")
    private boolean admin;

    /**
     * The status of the account.
     * By default all accounts are enabled.
     */
    @Column(name = "E_STATUS")
    @Enumerated
    private AccountStatus status = AccountStatus.ENABLED;

    /**
     * The timestamp of the last time this item was updated. Mapped to
     * D_UPDATED.
     */
    @Column(name = "D_UPDATED")
    @Temporal(value = TemporalType.TIMESTAMP)
    private Date updated;

    /**
     * The user id/software which has performed the last update. Mapped to
     * C_UPDATER.
     */
    @Column(name = "C_UPDATER", length = DbConstants.Generic.MODIFIED_SIZE)
    private String updater;

    /**
     * Implementation for Comparable interface, compares users based
     * on user ids.
     * @param otherUser the user object which this should be compared to.
     * @return the value returned by the compareTo of the ids of the users.
     */
    public int compareTo(final User otherUser) {
        return otherUser.getUserId().compareTo(userId);
    }

    /**
     * Override equals so that user objects are equal if their ids are equal.
     * @param obj the user object which this should be compared to.
     * @return true if the ids of the users are equal, false otherwise.
     */
    @Override
    public boolean equals(final Object obj) {
        return ((User) obj).getUserId().equals(userId);
    }

    /**
     * Override hashCode method.
     * @return the hashCode of the userId.
     */
    @Override
    public int hashCode() {
        return userId.hashCode();
    }

    /**
     * @return the userId
     */
    public final String getUserId() {
        return userId;
    }

    /**
     * @param userId the userId to set
     */
    public final void setUserId(final String userId) {
        this.userId = userId;
    }

    /**
     * @return the userName
     */
    public final String getUserName() {
        return userName;
    }

    /**
     * @param userName the userName to set
     */
    public final void setUserName(final String userName) {
        this.userName = userName;
    }

    /**
     * @return the password
     */
    public final String getPassword() {
        return password;
    }

    /**
     * @param password the password to set
     */
    public final void setPassword(final String password) {
        this.password = password;
    }

    /**
     * @return the email
     */
    public final String getEmail() {
        return email;
    }

    /**
     * @param email the email to set
     */
    public final void setEmail(final String email) {
        this.email = email;
    }

    /**
     * @return the language
     */
    public final String getLanguage() {
        return language;
    }

    /**
     * @param language the language to set
     */
    public final void setLanguage(final String language) {
        this.language = language;
    }

    /**
     * @return the admin
     */
    public final boolean isAdmin() {
        return admin;
    }

    /**
     * @param admin the admin to set
     */
    public final void setAdmin(final boolean admin) {
        this.admin = admin;
    }

    /**
     * @return the updated
     */
    public final Date getUpdated() {
        return updated;
    }

    /**
     * @param updated the updated to set
     */
    public final void setUpdated(final Date updated) {
        this.updated = updated;
    }

    /**
     * @return the updater
     */
    public final String getUpdater() {
        return updater;
    }

    /**
     * @param updater the updater to set
     */
    public final void setUpdater(final String updater) {
        this.updater = updater;
    }

    /**
     * Getter for the status.
     * @return the status.
     */
    public final AccountStatus getStatus() {
        return status;
    }

    /**
     * Setter for the status.
     * @param status the status to set.
     */
    public final void setStatus(final AccountStatus status) {
        this.status = status;
    }


}
